﻿namespace Dragon.Library.Framework.Bo
{
    public partial class BaseBo<T, TBean, TKey>
    {
        protected object Create(TBean tBean)
        {
            //if (!Dao.IsOpen)
            //    Dao.ConnectionOpen();
            InitializationSession();
            InitializationDao();
            object returnValue = Dao.Create(tBean);
            DisposeDao();
            DisposeSession();
            //if (Dao.IsOpen && !Dao.IsTran)
            //    Dao.ConnectionClose();
            //var newBean = tBean.Clone();
            //tBean = (TBean)newBean;
            return returnValue;
        }

        protected TBean Update(TBean tBean)
        {
            //if (!Dao.IsOpen)
            //    Dao.ConnectionOpen();
            InitializationSession();
            InitializationDao();
            Dao.Update(tBean);
            DisposeDao();
            DisposeSession();
            return tBean;
            //if (Dao.IsOpen && !Dao.IsTran)
            //    Dao.ConnectionClose();
            //var newBean = (TBean)tBean.Clone();
            //return newBean;
        }

        protected void Delete(TBean tBean)
        {
            //if (!Dao.IsOpen)
            //    Dao.ConnectionOpen();
            InitializationSession();
            InitializationDao();
            Dao.Delete(tBean);
            //if (Dao.IsOpen && !Dao.IsTran)
            //    Dao.ConnectionClose();
            DisposeDao();
            DisposeSession();
        }

        public void Flush()
        {
            if (Dao.IsTran)
            {
                Dao.Flush();
                //if (Dao.IsOpen)
                //    Dao.ConnectionClose();
                DisposeDao();
                DisposeSession();
            }
        }

        public void StartTran(string sessionKey)
        {
            Dao.Initialization(sessionKey);
            Dao.IsTran = true;
        }
    }
}
